#include "include.h"

using namespace std;
SDL_Surface *SDLFunctionsScreenGame = NULL;
SDL_Surface *SDLMessages = NULL;
SDL_Surface *SDLImages = NULL;
TTF_Font *SDLCFont = NULL;

const float DEG2RAD = 3.14159/180;

void onFunctionsStart(SDL_Surface* ScreenGame)
{
	SDLFunctionsScreenGame = ScreenGame;
}

bool FolderExists(string file)
{
	DWORD returnvalue;
	returnvalue = GetFileAttributes(file.c_str());
	if(returnvalue == ((DWORD)-1))
	{
		return false;
	}
	else
	{	
		return true;
	}
}

SDL_Surface *LImage( std::string filename )
{
	SDL_Surface* loadedImage = NULL;
	SDL_Surface* optimizedImage = NULL;
	loadedImage = IMG_Load( filename.c_str() );
	if( loadedImage != NULL )
	{
		optimizedImage = SDL_DisplayFormat( loadedImage );
		SDL_FreeSurface( loadedImage );
	}
	return optimizedImage;
}

bool drawImage(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip = NULL)
{
	SDL_Rect offset;
	offset.x = x;
	offset.y = y;
	if ( SDL_BlitSurface( source, clip, destination, &offset ) )
	{
		return true;
	}
	return false;
}

bool drawImage(int alpha, int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip = NULL)
{
	SDL_Rect offset;
	offset.x = x;
	offset.y = y;
	SDL_SetAlpha(source, SDL_SRCALPHA, alpha);
	if ( SDL_BlitSurface( source, clip, destination, &offset ) )
	{
		return true;
	}
	return false;
}

bool drawText(int red, int green, int blue, int alpha, string text, string filenamefont, string align, int size, int x, int y)
{
	SDLCFont = TTF_OpenFont(filenamefont.c_str(), size);
	
	if (red > 255) { red = 255; }
	if (green > 255) { green = 255; }
	if (blue > 255) { blue = 255; }
	if (alpha > 255) { alpha = 255; }
	SDL_Color scolors;
	SDL_Rect offset;
	int width = 0;
	int height = 0;
	scolors.r = red;
	scolors.g = green;
	scolors.b = blue;
	TTF_SizeText(SDLCFont, text.c_str(), &width, &height);
	if (align == "left") { offset.x = x-width; }
	else if (align == "center") { offset.x = x-width/2; }
	else if (align == "right") { offset.x = x; }
	offset.y = y;
	SDLMessages = TTF_RenderText_Blended( SDLCFont, text.c_str(), scolors );
	//SDL_SetAlpha(SDLMessages, SDL_SRCALPHA, alpha);
	if ( SDL_BlitSurface(SDLMessages, NULL, SDLFunctionsScreenGame, &offset) )
	{
		//SDL_Flip(SDLMessages);
		TTF_CloseFont(SDLCFont);
		return true;
	}
	return false;
}

void drawCircle(int x, int y, int radius, SDL_Surface *thescreen, int r, int g, int b)
{
	float floatx, floaty, floatradius;
	SDL_Rect trect;

	trect.w = 2;
	trect.h = 2;

	floatx = static_cast <float> (x);
	floaty = static_cast <float> (y);
	floatradius = static_cast <float> (radius);

	float i;

	for (i=0; i < 360; i+=.2)
	{
		float degInRad = i*DEG2RAD;

		trect.x = static_cast <int> (floatx + cos(degInRad) * floatradius);
		trect.y = static_cast <int> (floaty + sin(degInRad) * floatradius);

		if(trect.x >= thescreen->clip_rect.x)
			if(trect.y >= thescreen->clip_rect.y)
				if((trect.x + trect.w) <= thescreen->clip_rect.w)
					if((trect.y + trect.h) <= thescreen->clip_rect.h)
						SDL_FillRect(thescreen, &trect, SDL_MapRGB( thescreen->format,r,g,b));
	}

}

bool CheckMouseIsInCircle(int x, int y, int rad)
{
	int mouse_x, mouse_y;
	SDL_GetMouseState(&mouse_x, &mouse_y);

	int dx= mouse_x - x;
	int dy= mouse_y - y;
	if (dx < 0) { dx = dx - (dx * 2); }
	if (dy < 0) { dy = dy - (dy * 2); }
	float d1= dx * dx;
	float d2= dy * dy;
	float d3= d1+d2;
	float d = sqrt(d3);

	if (d <= rad)
		return true;
	else
		return false;

}

int GetFontWidth(string filename, string text, int size)
{
	int width = 0;
	int height = 0;
	SDLCFont = TTF_OpenFont(filename.c_str(), size);
	TTF_SizeText(SDLCFont, text.c_str(), &width, &height);
	return width;
}

string IntToString(int number)
{
	char buffer [33];
	itoa(number,buffer,10);
	return buffer;
}

string CharToString(char message)
{
	stringstream ss;
	string stringm;
	char c = message;
	ss << c;
	ss >> stringm;
	return stringm;
}